import redis
import pickle
from frameworks.cores.Config import *

class RedisUtil:
    r = None
    pool = None
    log_enable = 0
    enable = True

    def __init__(self):
        basepath = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
        config = Config(basepath)
        redisConf = config.getRedis()
        self.r = self.rc = redis.StrictRedis(host=redisConf[0],port=redisConf[2],db=redisConf[3],password=redisConf[1],decode_responses=True, charset='UTF-8', encoding='UTF-8')

    def setLogEnable(self,flag=0):
        self.log_enable = flag

    def setEnable(self,flag=True):
        self.enable = flag

    def log(self,name,set):
        if self.log_enable == 1:
            if set == 1:
                print("正在缓存" + name)
            else:
                print("取出缓存" + name)

    def set(self,name,key,val,ex=0):
        if self.enable == True:
            self.log(name,1)
            return self.r.hset(name,key,val)
        else:
            return False

    def setone(self,key,val,ex=0):
        if self.enable == True:
            return self.r.set(key,val)
        else:
            return False

    def getone(self,key):
        if self.enable == True:
            return self.r.get(key)
        else:
            return False

    def get(self,name,key):
        if self.enable == True:
            self.log(name,2)
            return self.r.hget(name,key)
        else:
            return False

    def setList(self,name,mapping):
        if self.enable == True:
            self.log(name,1)
            return self.r.hmset(name,mapping)
        else:
            return False

    def getList(self,name,args=""):
        if self.enable == True:
            self.log(name,2)
            return self.r.hmget(name,args)
        else:
            return False

    def getAllList(self,name):
        if self.enable == True:
            self.log(name,2)
            return self.r.hgetall(name)
        else:
            return False

    def delete(self,name):
        if self.enable == True:
            return self.r.delete(name)
        else:
            return False

    def exists(self,name):
        if self.enable == True:
            return self.r.exists(name)
        else:
            return False

    def close(self):
        if self.enable == True:
            return self.r.close()
        else:
            return False

    def llen(self,name):
        if self.enable == True:
            return self.r.llen(name)
        else:
            return False

    def setArray(self,name,lists):
        if self.enable == True:
            self.r.delete(name)
            self.log(name,1)
            for val in lists:
                self.r.lpush(name,str(val))
        else:
            return False

    def getArray(self,name):
        if self.enable == True:
            self.log(name,2)
            return self.r.lrange(name,0,self.r.llen(name))
        else:
            return False

    def flushall(self):
        if self.enable == True:
            self.r.flushall()
        else:
            return False